/**
 * 
 */
package dt.inlee.system.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import dt.inlee.common.constant.CommonConstant;
import dt.inlee.common.exception.FrameworkException;
import dt.inlee.domain.Message;
import dt.inlee.system.service.MessageService;
import dt.inlee.system.vo.MessageBean;
import dt.inlee.system.vo.SysUserLoginBean;

/**
 * @功能说明：消息提醒
 * @作者： xgyin
 * @创建日期： 2010-11-19
 */
@Scope("prototype")
@Results( { @Result(name = "success", type = "json") })
@Controller("messageAction")
public class MessageAction extends ActionSupport {
	private static final long serialVersionUID = -3480749568589498179L;
	Logger logger = Logger.getLogger(MessageAction.class);
	@Autowired
	private MessageService messageService;
	private List<MessageBean> messageBeans = new ArrayList<MessageBean>();
	private MessageBean messageBean;
	private boolean result = false;

	public List<MessageBean> getMessageBeans() {
		return messageBeans;
	}

	public void setMessageBeans(List<MessageBean> messageBeans) {
		this.messageBeans = messageBeans;
	}

	public boolean isResult() {
		return result;
	}

	public void setResult(boolean result) {
		this.result = result;
	}

	public MessageBean getMessageBean() {
		return messageBean;
	}

	public void setMessageBean(MessageBean messageBean) {
		this.messageBean = messageBean;
	}

	/**
	 * 查询登录人自己未处理的任务
	 * 
	 * @return
	 * @throws FrameworkException
	 */
	@Action(value = "getPersonNotReadMsg", results = { @Result(type = "json") })
	public String getPersonNotReadMsg() throws FrameworkException {
		//登陆用户信息
		SysUserLoginBean loginUser = (SysUserLoginBean) ActionContext.getContext().getSession().get(
				CommonConstant.SESSION_USER);
		List<Message> messages = this.messageService.getPersonNotReadMsg(loginUser.getId()+"");
		for (Message message : messages) {
			MessageBean bean = new MessageBean();
			BeanUtils.copyProperties(message, bean);
			this.messageBeans.add(bean);
		}
		if (this.messageBeans.size() > 0)
			this.result = true;
		return SUCCESS;

	}

	/**
	 * 读取消息
	 * @return
	 * @throws FrameworkException
	 */
	@Action(value = "readMsg", results = { @Result(type = "json", params = { "excludeNullProperties", "true",
			"includeProperties", "result" }) })
	public String readMsg() throws FrameworkException {
		this.messageService.readMsg(this.messageBean.getId());
		this.result = true;
		return SUCCESS;

	}

}
